<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <!-- 搜索框 -->
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="searchFn"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          default-expand-all
          :data="tree"
          :props="defaultProps"
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNodeChange"
        >
          <template #default="{ data }">
            <el-row style="width: 100%; height:40px" align="middle" type="flex" justify="space-between">
              <span>{{ data.name }}</span>
            </el-row>
          </template>
        </el-tree>
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-permission="'employee:add'" size="mini" type="primary" @click="addFn">添加员工</el-button>
          <el-button v-permission="'employee:import:excel'" size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button v-permission="'employee:export:excel'" size="mini" @click="exportExcel">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template #default="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username?.charAt(2,4) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{ row }">
              <span>{{ row.formOfEmployment===1?'正式':'非正式' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="editRoleFn(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="delFn(row)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm></template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination layout="prev, pager, next" :total="total" @current-change="changepage" />
        </el-row>
      </div>
      <ImportExcel :show-excel-dialog.sync="showExcelDialog" />
      <el-dialog :visible.sync="showRoleDialog" title="分配角色">
        <!-- 弹层内容 -->
        <!-- checkbox -->
        <el-checkbox-group v-model="roleIds">
          <!-- 放置n个的checkbox  要执行checkbox的存储值 item.id-->
          <el-checkbox
            v-for="item in roleList"
            :key="item.id"
            :label="item.id"
          >{{ item.name }}</el-checkbox>
        </el-checkbox-group>
        <div class="btn-box">
          <el-button type="primary" size="mini" @click="confirmBtnFn">确认</el-button>
          <el-button size="mini" @click="showRoleDialog = false">取消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getdepartment } from '@/api/department'
import { getEmployeeList, exportEmployee, delEmployee, editEmployee } from '@/api/employee'
import { translateDataToTree } from '@/utils'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'
import { assignPerm, getRoleListAll } from '@/api/role'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      tree: [],
      list: [],
      id: '',
      roleList: [], // 放置角色列表
      roleIds: [], // 双向绑定角色id
      showExcelDialog: false, // 控制excel弹窗的显示与隐藏
      timer: null,
      showDialog: false, // 控制弹窗的显示与隐藏
      showRoleDialog: false, // 控制分配角色弹窗的显示与隐藏
      currentId: null,
      typeName: '',
      total: 0,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryParams: {
        page: 1,
        pagesize: 10,
        keyword: '',
        departmentId: null
      }
    }
  },
  created() {
    this.getData()
    this.getEmployeeList()
  },
  methods: {
    // 分配角色
    async editRoleFn(id) {
      this.roleList = await getRoleListAll()
      const res = await editEmployee(id)
      // console.log(res)
      this.roleIds = res.roleIds
      this.id = id
      this.showRoleDialog = true
    },
    // 确认添加角色列表
    async confirmBtnFn() {
      // console.log(this.roleIds, this.id)
      const data = { id: this.id, permIds: this.roleIds }
      await assignPerm(data)
      this.$message.success('分配角色成功')
      this.showRoleDialog = false
      this.getEmployeeList()
    },
    async getData() {
      const res = await getdepartment()
      this.tree = translateDataToTree(res, 0) // 将数据转换为树形结构
      this.queryParams.departmentId = this.tree[0].id
      this.$nextTick(() => {
        // 设置当前节点
        this.$refs.tree.setCurrentKey(this.queryParams.departmentId)
      })
    },
    // 树节点点击事件,点击节点时触发
    selectNodeChange(node) {
      this.queryParams.departmentId = node.id
      this.getEmployeeList()
    },
    // 获取全部渲染
    async getEmployeeList() {
      const res = await getEmployeeList(this.queryParams)
      // console.log(res)
      this.list = res.rows
      this.total = res.total
    },
    changepage(val) {
      this.queryParams.page = val
      this.getEmployeeList()
    },
    addFn() {
      // console.log('添加员工')
      this.$router.push('/employee/detail')
    },
    async delFn(row) {
      // console.log(row.id)
      await delEmployee(row.id)
      // console.log(res)
      this.getEmployeeList()
      this.$message.success('删除成功')
    },
    // 搜索框搜索
    searchFn() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        // console.log(this.queryParams)
        this.getEmployeeList()
      }, 800)
    },
    async exportExcel() {
      console.log('导出')
      const res = await exportEmployee()
      // console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .btn-box {
    text-align: center;
    padding: 20px;
  }
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }

  .right {
    flex: 1;
    padding: 20px;

    .opeate-tools {
      margin: 10px;
    }

    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
